#include "settabwidgetpage.h"
#include <QString>
#include <QHeaderView>
#include "tabledefine.h"

extern QString projectDB;

void SetTabWidgetPage::setView(QTableView *view)
{
    view->setSelectionBehavior(QAbstractItemView::SelectRows);  //设置选中时选中一行
    view->hideColumn(0);  //隐藏id列(第0列)
    view->resizeColumnsToContents();
    view->verticalHeader()->hide();
    view->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//均分列
}

void SetTabWidgetPage::setPageLayout(QGridLayout *layout, QWidget *page, QTableView *view)
{
    layout->addWidget(view);
    page->setLayout(layout);
}

void SetTabWidgetPage::setBuildingWidget(QSqlRelationalTableModel *model, QTableView *view, QString filter)
{
    model->setTable(BUILDING_TABLE_NAME);
    model->setHeaderData(0, Qt::Horizontal, "建筑id");
    model->setHeaderData(1, Qt::Horizontal, "名称");
    model->setHeaderData(2, Qt::Horizontal, "描述");
    model->setHeaderData(3, Qt::Horizontal, "序号");
    model->setHeaderData(4, Qt::Horizontal, "上级id");
    model->setHeaderData(5, Qt::Horizontal, "建筑类型");
    if(!filter.isEmpty())
        model->setFilter(filter);
    model->select();
    
    view->setModel(model);
    view->hideColumn(4);
    view->hideColumn(5);
    
    setView(view);
}

void SetTabWidgetPage::setFloorWidget(QSqlRelationalTableModel *model, QTableView *view, QString filter)
{
    model->setTable(BUILDING_TABLE_NAME);
    model->setHeaderData(0, Qt::Horizontal, "建筑id");
    model->setHeaderData(1, Qt::Horizontal, "名称");
    model->setHeaderData(2, Qt::Horizontal, "描述");
    model->setHeaderData(3, Qt::Horizontal, "序号");
    model->setHeaderData(4, Qt::Horizontal, "上级id");
    model->setHeaderData(5, Qt::Horizontal, "建筑类型");
    if(!filter.isEmpty())
        model->setFilter(filter);
    model->select();
    
    view->setModel(model);
    view->hideColumn(4);
    view->hideColumn(5);
    
    setView(view);
}

void SetTabWidgetPage::setRoomWidget(QSqlRelationalTableModel *model, QTableView *view, QString filter)
{
    model->setTable(BUILDING_TABLE_NAME);
    model->setHeaderData(0, Qt::Horizontal, "建筑id");
    model->setHeaderData(1, Qt::Horizontal, "名称");
    model->setHeaderData(2, Qt::Horizontal, "描述");
    model->setHeaderData(3, Qt::Horizontal, "序号");
    model->setHeaderData(4, Qt::Horizontal, "上级id");
    model->setHeaderData(5, Qt::Horizontal, "建筑类型");
    if(!filter.isEmpty())
        model->setFilter(filter);
    model->select();
    
    view->setModel(model);
    view->hideColumn(4);
    view->hideColumn(5);
    
    setView(view);
}

void SetTabWidgetPage::setDeviceWidget(QSqlRelationalTableModel *model, QTableView *view, QString filter)
{        
    model->setTable(DEVICE_TABLE_NAME);
    model->setHeaderData(0, Qt::Horizontal, "设备id");
    model->setHeaderData(1, Qt::Horizontal, "产品id");
    model->setHeaderData(2, Qt::Horizontal, "房间id");
    model->setHeaderData(3, Qt::Horizontal, "名称");
    model->setHeaderData(4, Qt::Horizontal, "物理地址");
    model->setHeaderData(5, Qt::Horizontal, "应用程序");
    model->setHeaderData(6, Qt::Horizontal, "Adr");
    model->setHeaderData(7, Qt::Horizontal, "Prg");
    model->setHeaderData(8, Qt::Horizontal, "Par");
    model->setHeaderData(9, Qt::Horizontal, "Grp");
    model->setHeaderData(10, Qt::Horizontal, "Cfg");
    model->setHeaderData(11, Qt::Horizontal, "厂家");
    model->setHeaderData(12, Qt::Horizontal, "订货号");
    model->setHeaderData(13, Qt::Horizontal, "产品信息");
    model->setHeaderData(14, Qt::Horizontal, "设备型号");
    model->setHeaderData(15, Qt::Horizontal, "出厂日期");
    model->setHeaderData(16, Qt::Horizontal, "设备版本号");
    model->setHeaderData(17, Qt::Horizontal, "设备数据库文件名");
    model->setHeaderData(18, Qt::Horizontal, "设备序列号");
    model->setHeaderData(19, Qt::Horizontal, "描述");
    if(!filter.isEmpty())
        model->setFilter(filter);
    model->select();
    
    view->setModel(model);
    view->hideColumn(1);
    view->hideColumn(2);
    view->hideColumn(17);
    view->hideColumn(18);
    
    setView(view);
}

void SetTabWidgetPage::setParameterWidget(QSqlRelationalTableModel *model, QTableView *view, QString filter)
{
    //参数界面主要为参数相关界面内容
    
    if(!filter.isEmpty())
        model->setFilter(filter);
    
    view->setModel(model);
    
    setView(view);
}

void SetTabWidgetPage::setFuncWidget(QSqlRelationalTableModel *model, QTableView *view, QString filter)
{
    model->setTable(BUILDING_TABLE_NAME);
    model->setHeaderData(0, Qt::Horizontal, "建筑id");
    model->setHeaderData(1, Qt::Horizontal, "名称");
    model->setHeaderData(2, Qt::Horizontal, "描述");
    model->setHeaderData(3, Qt::Horizontal, "序号");
    model->setHeaderData(4, Qt::Horizontal, "上级id");
    model->setHeaderData(5, Qt::Horizontal, "建筑类型");
    if(!filter.isEmpty())
        model->setFilter(filter);
    model->select();
    
    view->setModel(model);
    view->hideColumn(4);
    view->hideColumn(5);
    
    setView(view);
}

void SetTabWidgetPage::setGroupObjectWidget(QSqlRelationalTableModel *model, QTableView *view, QString filter)
{
    model->setTable(COMM_OBJECT_TABLE_NAME);
    model->setHeaderData(0, Qt::Horizontal, "id");
    model->setHeaderData(1, Qt::Horizontal, "comm_object_id");
    model->setHeaderData(2, Qt::Horizontal, "product_id");
    model->setHeaderData(3, Qt::Horizontal, "device_id");
    model->setHeaderData(4, Qt::Horizontal, "eeprom_address");
    model->setHeaderData(5, Qt::Horizontal, "序号");
    model->setHeaderData(6, Qt::Horizontal, "对象功能");
    model->setHeaderData(7, Qt::Horizontal, "C");
    model->setHeaderData(8, Qt::Horizontal, "R");
    model->setHeaderData(9, Qt::Horizontal, "W");
    model->setHeaderData(10, Qt::Horizontal, "V");
    model->setHeaderData(11, Qt::Horizontal, "T");
    model->setHeaderData(12, Qt::Horizontal, "U");
    model->setHeaderData(13, Qt::Horizontal, "数据类型");
    model->setHeaderData(14, Qt::Horizontal, "优先级");
    model->setHeaderData(15, Qt::Horizontal, "关联的组地址");
    model->setHeaderData(16, Qt::Horizontal, "描述");
    if(!filter.isEmpty())
        model->setFilter(filter);
    model->select();
    
    view->setModel(model);
    view->hideColumn(1);
    view->hideColumn(2);
    view->hideColumn(3);
    view->hideColumn(4);
    
    setView(view);
}

void SetTabWidgetPage::setObjectRelationWidget(QSqlRelationalTableModel *model, QTableView *view, QString filter)
{
    model->setTable(RELATION_TABLE_NAME);
    model->setHeaderData(0, Qt::Horizontal, ID);
    model->setHeaderData(1, Qt::Horizontal, RELATION_ID);
    model->setHeaderData(2, Qt::Horizontal, "产品id");
    model->setHeaderData(3, Qt::Horizontal, "设备id");
    model->setHeaderData(4, Qt::Horizontal, "eeprom地址");
    model->setHeaderData(5, Qt::Horizontal, "关联序号");
    model->setHeaderData(6, Qt::Horizontal, "通讯对象序号");
    model->setHeaderData(7, Qt::Horizontal, "地址序号");
    if(!filter.isEmpty())
        model->setFilter(filter);
    model->select();
    
    view->setModel(model);
    
    setView(view);
}

void SetTabWidgetPage::setMainGroupWidget(QSqlRelationalTableModel *model, QTableView *view, QString filter)
{
    model->setTable(ADDRESS_TABLE_NAME);
    model->setHeaderData(0, Qt::Horizontal, ID);
    model->setHeaderData(1, Qt::Horizontal, ADDRESS_ID);
    model->setHeaderData(2, Qt::Horizontal, "值");
    model->setHeaderData(3, Qt::Horizontal, "名称");
    model->setHeaderData(4, Qt::Horizontal, "描述");
    model->setHeaderData(5, Qt::Horizontal, "上级id");
    model->setHeaderData(6, Qt::Horizontal, "群组地址");
    model->setHeaderData(7, Qt::Horizontal, "产品id");
    model->setHeaderData(8, Qt::Horizontal, "设备id");
    model->setHeaderData(9, Qt::Horizontal, "eeprom地址");
    model->setHeaderData(10, Qt::Horizontal, "地址序号");
    model->setHeaderData(11, Qt::Horizontal, "类型");
    if(!filter.isEmpty())
        model->setFilter(filter);
    model->select();
    
    view->setModel(model);
    
    setView(view);
}

void SetTabWidgetPage::setMiddleGroupWidget(QSqlRelationalTableModel *model, QTableView *view, QString filter)
{
    model->setTable(ADDRESS_TABLE_NAME);
    model->setHeaderData(0, Qt::Horizontal, ID);
    model->setHeaderData(1, Qt::Horizontal, ADDRESS_ID);
    model->setHeaderData(2, Qt::Horizontal, "值");
    model->setHeaderData(3, Qt::Horizontal, "名称");
    model->setHeaderData(4, Qt::Horizontal, "描述");
    model->setHeaderData(5, Qt::Horizontal, "上级id");
    model->setHeaderData(6, Qt::Horizontal, "群组地址");
    model->setHeaderData(7, Qt::Horizontal, "产品id");
    model->setHeaderData(8, Qt::Horizontal, "设备id");
    model->setHeaderData(9, Qt::Horizontal, "eeprom地址");
    model->setHeaderData(10, Qt::Horizontal, "地址序号");
    model->setHeaderData(11, Qt::Horizontal, "类型");
    if(!filter.isEmpty())
        model->setFilter(filter);
    model->select();
    
    view->setModel(model);
    
    setView(view);
}

void SetTabWidgetPage::setGAWidget(QSqlRelationalTableModel *model, QTableView *view, QString filter)
{
    model->setTable(ADDRESS_TABLE_NAME);
    model->setHeaderData(0, Qt::Horizontal, ID);
    model->setHeaderData(1, Qt::Horizontal, ADDRESS_ID);
    model->setHeaderData(2, Qt::Horizontal, "值");
    model->setHeaderData(3, Qt::Horizontal, "名称");
    model->setHeaderData(4, Qt::Horizontal, "描述");
    model->setHeaderData(5, Qt::Horizontal, "上级id");
    model->setHeaderData(6, Qt::Horizontal, "群组地址");
    model->setHeaderData(7, Qt::Horizontal, "产品id");
    model->setHeaderData(8, Qt::Horizontal, "设备id");
    model->setHeaderData(9, Qt::Horizontal, "eeprom地址");
    model->setHeaderData(10, Qt::Horizontal, "地址序号");
    model->setHeaderData(11, Qt::Horizontal, "类型");
    if(!filter.isEmpty())
        model->setFilter(filter);
    model->select();
    
    view->setModel(model);
    
    setView(view);
}

void SetTabWidgetPage::setGARelationWidget(QSqlRelationalTableModel *model, QTableView *view, QString filter)
{
    model->setTable(RELATION_TABLE_NAME);
    model->setHeaderData(0, Qt::Horizontal, ID);
    model->setHeaderData(1, Qt::Horizontal, RELATION_ID);
    model->setHeaderData(2, Qt::Horizontal, "产品id");
    model->setHeaderData(3, Qt::Horizontal, "设备id");
    model->setHeaderData(4, Qt::Horizontal, "eeprom地址");
    model->setHeaderData(5, Qt::Horizontal, "关联序号");
    model->setHeaderData(6, Qt::Horizontal, "通讯对象序号");
    model->setHeaderData(7, Qt::Horizontal, "地址序号");
    if(!filter.isEmpty())
        model->setFilter(filter);
    model->select();
    
    view->setModel(model);
    
    setView(view);
}
